// eslint-disable-next-line
/* eslint-disable */
import { MessageBox } from 'mint-ui'
export const mixin = {
	// 混入对象
	methods: {
		// 设置用户头像
		setImg(){
			this.$refs.common.$refs.file.click()
		},
		// 上传预览图片
		readURL(file) {
			this.file = file
			const that = this
			const input = this.$refs.common.$refs.file
		   if (input.files && input.files[0]) {
		       var reader = new FileReader();
		       reader.onload = function (e) {
		          that.$refs.common.$refs.preview.src = e.target.result;
		       }
		       reader.readAsDataURL(input.files[0]);
		   }
		},
	}
}
// eslint-disable-next-line
/* eslint-disable */
// 获取用户信息
export const mixinGetUserInfoList = {
	created() {
		this.getUserInfoList()
    },
    methods: {
    	getUserInfoList(){
    		// 在数据加载成功之前，显示加载字样
    		this.loading = true
            this.moreShow = true
    		api.showinfo({
    			user_id: localStorage.getItem('userid'),
    			token: localStorage.getItem("token"),
    			page: this.page,
    			count: this.count
    		}).then(res=>{
    			if(res.code===0){
                    // 合并数据
    				this.userInfos = this.userInfos.concat(this.handleData(res));
                    this.originInfos = this.originInfos.concat(res.data);
    				// 在数据加载完成时候，隐藏加载字样
    				this.loading = false
                	this.moreShow = false
    			}
    		}).catch(res=>{
    			if(res.code===1005){
    				this.loading = true
                	this.moreShow = true
                	this.moreVal = '无更多内容'
    			}
    			
    		})

    	},
    	handleData(res){
    		var userInfos = res.data;
    		if(!userInfos.length){
    			return ;
    		}
    		var i = 0,
    			l = userInfos.length,
    			data = [];
    		for(;i<l;i++){
    			data.push({
    				id: userInfos[i].Id,
    				path: res.user_img,
    				date: userInfos[i].date 
    			})
    		}
    		return data;
    	},
    }
}

// 编辑内容
export const getEditInfo = {
    created() {
        this.getEditInfo();
    },
    methods: {
        getEditInfo() {
            api.editinfo({
                user_id: localStorage.getItem("userid"),
                user_name: localStorage.getItem("username"),
                token: localStorage.getItem("token"),
                id: this.$route.params.id,
            }).then(res=>{
                console.log('res',res)
                if(res.code===0){
                    var oEditInfo = this.$refs.editinfo.$refs
                    oEditInfo.date.value = res.date
                    oEditInfo.morning.value = res.morning
                    oEditInfo.afternoon.value = res.afternoon
                    oEditInfo.evening.value = res.evening
                    oEditInfo.other.value = res.other
                }
            }).catch(err=>{
                if(err.code===1006){
                    MessageBox("",err.message).then(action=>{
                        if(action=="confirm"){
                            this.$router.go(-1)
                        }
                    })
                }
            })
        }
    }
}



export const account = {
    methods: {
        account(date,morning,afternoon,evening,other) {
            if(!date){
                MessageBox("","请选择日期")
                return;
            }
            if(!morning){
                MessageBox("","请输入早餐花费")
                return;
            }
            if(!afternoon){
                MessageBox("","请输入午餐花费")
                return;
            }
            if(!evening){
                MessageBox("","请输入晚餐花费")
                return;
            }
            if(!other){
                MessageBox("","请输入其他花费")
                return;
            }
            api.addinfo({
                date: date,
                morning: morning,
                afternoon: afternoon,
                evening: evening,
                other: other,
                user_id: localStorage.getItem('userid'),
                token: localStorage.getItem('token')
            }).then(res=>{
                if(res.code===0){
                    MessageBox.confirm(res.message+"，并去查看？").then(action=>{
                        if(action=="confirm"){
                            this.$router.push({
                                name: 'showinfo'
                            })
                        }else{
                            this.$router.push({
                                name: 'addinfo'
                            })
                        }
                    })
                }
                
            }).catch(res=>{
                if(res.code===1004){
                    MessageBox("",res.message)
                }
                
            })
        }
    }
}

// 编辑内容之后提交
export const postEditSubmit = {
    methods: {
        editSubmit() {
            var oEditInfo = this.$refs.editinfo.$refs
            var date = oEditInfo.date.value,
                morning = oEditInfo.morning.value,
                afternoon = oEditInfo.afternoon.value,
                evening = oEditInfo.evening.value,
                other = oEditInfo.other.value;
                if(!date){
                MessageBox("","请选择日期")
                return;
                }
                if(!morning){
                    MessageBox("","请输入早餐花费")
                    return;
                }
                if(!afternoon){
                    MessageBox("","请输入午餐花费")
                    return;
                }
                if(!evening){
                    MessageBox("","请输入晚餐花费")
                    return;
                }
                if(!other){
                    MessageBox("","请输入其他花费")
                    return;
                }
            api.editsubmit({
                date: date,
                morning: morning,
                afternoon: afternoon,
                evening: evening,
                other: other,
                user_id: localStorage.getItem('userid'),
                token: localStorage.getItem('token'),
                id: this.$route.params.id,
            }).then(res=>{
                // console.log("editSubmit",res)
                if(res.code===0){
                   MessageBox.confirm(res.message+", 并去查看？").then(action=>{
                    if(action=="confirm"){
                        if(action=="confirm"){
                            this.$router.push({
                                name: 'showinfo'
                            })
                        }else{
                            this.$router.push({
                                name: 'addinfo'
                            })
                        }
                    }
                   }) 
                }
                
            }).catch(err=>{
                if(err.code===1007){
                    MessageBox("",err.message)
                }
            })
        }
    }
}